﻿using MS_Entity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace MS_UI
{
    public partial class FrmTrend : Form
    {
        public FrmTrend()
        {
            InitializeComponent();
            this.timer1.Interval = 1000;
            this.Load += FrmTrend_Load;
        }
        int count = 20;
        private void FrmTrend_Load(object sender, EventArgs e)
        {
            // 初始化ComboBox下拉菜单
            this.cmb_ThrendType.Items.Add("实时趋势");
            this.cmb_ThrendType.Items.Add("历史趋势");
            this.cmb_ThrendType.SelectedIndex = 0;
            this.cmb_ZoneSel.Items.Add("冷却水区域");
            this.cmb_ZoneSel.Items.Add("压缩空气区域");
            this.cmb_ZoneSel.SelectedIndex = 0;
            // 初始化chart
            this.chart1.Series.Clear();
            Series objSeries = new Series("冷却塔液位");
            objSeries.ChartType = SeriesChartType.Spline;
            objSeries.BorderWidth = 3;
            objSeries.XValueType = ChartValueType.DateTime;
            this.chart1.Series.Add(objSeries);
            //this.chk1_1.Checked = true;
            this.timer1.Enabled = true;
            count = int.Parse(this.textBox2.Text.Trim());
        }

        private void cmb_ZoneSel_SelectedIndexChanged(object sender, EventArgs e)
        {
            string str = "" +     (this.cmb_ZoneSel.SelectedIndex + 1).ToString(); ;
            foreach (Control ct in this.LeftPanel.Controls)
            {
                if (ct is CheckBox)
                {
                    CheckBox chb = (CheckBox)ct;
                    if (ct.Name.Contains(str))
                    {
                        ct.Visible = true;
                    }
                    else
                    {
                        ct.Visible = false;
                    }
                }
            }
        }

        private void btn_sure_Click(object sender, EventArgs e)
        {
            this.chart1.Series.Clear();
            foreach (Control item in this.LeftPanel.Controls)
            {
                if (item is CheckBox)
                {
                    CheckBox chb = (CheckBox)item;
                    if (chb.Checked)
                    {
                        if (this.Tag != null)
                        {
                            string VarNote = "测试";
                            if (chb.Tag != null)
                            {
                                if (CommonMethod.CurrentVarNote.ContainsKey(chb.Tag.ToString()))
                                {
                                    VarNote = CommonMethod.CurrentVarNote[chb.Tag.ToString()];
                                }
                                Series objSeries = new Series(VarNote);
                                objSeries.ChartType = SeriesChartType.Spline;
                                objSeries.BorderWidth = 3;
                                objSeries.XValueType = ChartValueType.DateTime;
                                this.chart1.Series.Add(objSeries);
                            }
                        }
                    }
                }
            }
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            foreach (Control item in this.LeftPanel.Controls)
            {
                if (item is CheckBox)
                {
                    CheckBox chb = (CheckBox)item;
                    chb.Checked = false; 
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            foreach (Series var in this.chart1.Series)
            {
                List<DateTime> X = new List<DateTime>();
                List<string> Y = new List<string>();
                int index = CommonMethod.FileVarNameList.IndexOf(var.Name);
                int start = 0;
                int end = 0;
                int actualcache = CommonMethod.CacheList.Count;
                start = actualcache <= count ? 0 : (actualcache - count);
                end = actualcache <= count ? actualcache : actualcache;
                for (int i = start; i < end; i++)
                {
                    List<Cache> list = CommonMethod.CacheList[i];
                    X.Add(list[index].InserTime);
                    Y.Add(list[index].Value);
                }
                var.IsValueShownAsLabel = count <= 30 ? true : false;
                var.Points.DataBindXY(X,Y);
            }
            //foreach (List<Cache> item in CommonMethod.CacheList)
            //{
            //    X.Add(item[0].InserTime);
            //    Y.Add(item[0].Value);
            //}
            //Series objSeries = new Series("冷却塔液位");
            //objSeries.ChartType = SeriesChartType.Spline;
            //objSeries.BorderWidth = 3;
            //objSeries.IsValueShownAsLabel = true;
            //objSeries.XValueType = ChartValueType.DateTime;
            //this.chart1.ChartAreas["ChartArea1"].AxisX.LabelStyle.Format = "HH:mm:ss";
            //this.chart1.Series.Add(objSeries);
            //this.chart1.Series[0].Points.DataBindXY(X,Y);
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            if (DataValidate.IsInteger(this.textBox2.Text.Trim()))
            {
                MessageBox.Show("必须填入整数","填入错误");
                return;
            }
            if (int.Parse(this.textBox2.Text.Trim())> CommonMethod.CacheCount)
            {
                MessageBox.Show("填入数据不得大于"+CommonMethod.CacheCount.ToString(), "填入错误");
                return;
            }
            count = int.Parse(this.textBox2.Text.Trim());
        }
    }
}
                                                          